using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BO.SMS;
using System.Collections.Generic;

namespace WebSMS
{
    public partial class SMS : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string DATAHOJE = DateTime.Today.Year.ToString() + "_" + DateTime.Today.Month.ToString() + "_" + DateTime.Today.Day.ToString();
            string path = Request.PhysicalApplicationPath + "message" + DATAHOJE + ".txt";
            string pathErro = Request.PhysicalApplicationPath + "\\errorlog.txt";
            try
            {
                #region variaveis
                string user = Request["User"];
                string pwd = Request["password"];
                string celular = Request["celular"];
                string message = Request["mensagem"];
                bool chamar = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["ExecutarChamada"]);
                //Response.Write(user);
                string url = "ERRO";
                #endregion
                #region ips
                string hotnam = System.Net.Dns.GetHostName();
                #region teste ips
                /*Response.Write("HostNameDNS:");
                        Response.Write("<br>");
                        Response.Write(hotnam);
                        Response.Write("<br>");

                        hotnam = Request.UserHostName;
                        Response.Write("Request.UserHostName:");
                        Response.Write("<br>");
                        Response.Write(hotnam);
                        Response.Write("<br>");*/
                #endregion
                System.Net.IPAddress[] ips = System.Net.Dns.GetHostAddresses(hotnam);
                List<string> Ips = new List<string>();
                foreach (System.Net.IPAddress ip in ips)
                {
                    /*Response.Write("System.Net.IPAddress:");
                    Response.Write("<br>");
                    Response.Write(ip.ToString());
                    Response.Write("<br>");*/
                    Ips.Add(ip.ToString());
                }

                #region testes ips
                /*
            
                        Response.Write("HttpContext.Current.Request.ServerVariables[REMOTE_ADDR]:");
                        Response.Write(HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
                        Response.Write("<br>");
                        Response.Write("Request.UserHostAddress");
                        Response.Write("<br>");
                        Response.Write(Request.UserHostAddress.ToString());
                        Response.Write("Request.ServerVariables[REMOTE_ADDR]");
                        Response.Write("<br>");
                        Response.Write(Request.ServerVariables["REMOTE_ADDR"]);*/
                #endregion
                #endregion

                clsBOSMS BOSMS = new clsBOSMS();
                url = BOSMS.insert_Message_cel(celular, message, user, pwd, Ips, pathErro, path);
                
                
         
                #region validando resposta
                //txtResult.Text = url;
                if (url.ToUpper() == "ERRO")
                {
                    Response.Write("0");
                }
                else
                {
                    //Response.Write(url);
                    if (chamar)
                    {
                        Response.Write("1");
                        #region testes
                        /*string senha = Tools.cryp.Cryptografia.Encriptar("lopesAutorizado"+DateTime.Today.Hour.ToString(),Tools.cryp.Crypto.TipoChave.Password);
                    string UrlCripto = Tools.cryp.Cryptografia.Encriptar(celular + ";" + message, Tools.cryp.Crypto.TipoChave.Password);
                    path = Tools.cryp.Cryptografia.Encriptar(path, Tools.cryp.Crypto.TipoChave.Password);*/
                        #region wbs
                        /*WBSSMS.sms WbsSMS = new WebSMS.WBSSMS.sms();
                    WbsSMS.EnviaResposta(senha, UrlCripto,path);*/
                        #endregion
                        #region server execute
                        //Server.Execute("smsRetorno.aspx?senha=" + senha+"&url="+UrlCripto);
                        #endregion
                        #region scriptcliente
                        /*string endStr = "</";
                    string strJScript = "";

                    strJScript = "<script language=\"JavaScript\">";
                    
                    strJScript += "window.open(\"smsRetorno.aspx?senha=" + senha+"&url="+UrlCripto+"\",\"myWindow\",\"top=0,left=0,w idth=790,height=500,buttons=no,scrollbars=yes,loca tion=no,menubar=no,resizable=no,status=no,director ies=no,toolbar=no\");";
                    
                    strJScript += endStr + "script>";
                    ClientScript.RegisterClientScriptBlock( this.GetType(),"clientScript", strJScript,false);
                    */
                        //Page.RegisterClientScriptBlock("clientScript", strJScript);
                        #endregion
                        #region redirect
                        //Response.Redirect(url);
                        #endregion
                        #endregion
                    }
                    else
                    {
                        Response.Write("0");
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                Tools.clsDefault.trataerro(ex, pathErro);
            }
        }
    }
}
